//
//  HMNewFeatureViewController.swift
//  Weibo
//
//  Created by 米易雅克 on 16/4/14.
//  Copyright © 2016年 米易雅克. All rights reserved.
//

import UIKit
import SnapKit
private let reuseIdentifier = "Cell"

class HMNewFeatureViewController: UICollectionViewController {
//布局参数流水布局
    let layout = UICollectionViewFlowLayout()
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    init(){
    
        super.init(collectionViewLayout: layout)
        
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
    
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
//        注册cell
     self.collectionView!.registerClass(HMFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
    }
    
//    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return numberOfNewFeatureItem
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! HMFeatureCell
        
        cell.index = indexPath.item
        
        return cell
        
    }
    
}
//MARK: 扩展HMNewFeatureViewController实现scrollViewDidEndDecelerating方法
extension HMNewFeatureViewController{

//scrollView停止滚动时调用
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let  index = Int(scrollView.contentOffset.x/UIScreen.mainScreen().bounds.width)
//    判断是否是最后一页
        if index == numberOfNewFeatureItem - 1 {
            let indexPath = NSIndexPath(forItem: index,inSection: 0)
//            如果最后一页让cell开始动画
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as!HMFeatureCell
            cell.startAnimation()
            
        }
        
        
    }


}
class HMFeatureCell: UICollectionViewCell {
    var index: Int = 0{
    
        didSet{
        
        
//  当控制器给cell的Index设置后
//    拼接图片
            let imageName = "new_feature_\(index+1)"
            
        bgImage.image = UIImage.init(named:imageName)
//            隐藏按钮
            startButton.hidden = true
        }
    
    
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
//    系统会来调用这个方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    private func prepareUI(){
        
        
//添加子控件
    contentView.addSubview(bgImage)
    contentView.addSubview(startButton)
//  添加约束
     bgImage.snp_makeConstraints(closure: { (make) in
       make.edges.equalTo(UIEdgeInsetsZero)
     })
        startButton.snp_makeConstraints(closure: { (make) in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-160)
        })
        
    }
    
    
    
//MARK: 动画方法
    func startAnimation() {
        
//      显示按钮
        startButton.hidden = false
//        将button缩放至零
        startButton.transform = CGAffineTransformMakeScale(0, 0)
//      开始动画
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: {
//            回到初始值
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) in
                HMPrint("动画成功")
        }
        
    }
//    开始按钮点击方法
   @objc private func didClickStartButton(){
    
        let controller = HMMainViewController()
        (UIApplication.sharedApplication().delegate as! AppDelegate).switchViewController(controller)
        
    
    
    
    }
    
// MARK: 懒加载控件
    private lazy var bgImage:UIImageView = UIImageView(image: UIImage.init(named: "new_feature_1"))
    
    private lazy var startButton:UIButton = {
    
    let button = UIButton()
    button.setBackgroundImage(UIImage.init(named: "new_feature_finish_button"), forState: UIControlState.Normal)
    button.setBackgroundImage(UIImage.init(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Normal)
    button.setTitle("开始体验", forState: UIControlState.Normal)
    button.titleLabel?.font = UIFont.systemFontOfSize(HMNewFeatureLabelFontSize)
    button.sizeToFit()
    
//    添加按钮点击事件
   button.addTarget(self, action: #selector(HMFeatureCell.didClickStartButton), forControlEvents: UIControlEvents.TouchUpInside)
        
    return button
    }()
    
}
